package com.yeyks.controller.other;

import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.account.utils.AccountUtil;
import com.yeyks.annotation.Login;
import com.yeyks.annotation.NeedToken;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.base.PageResult;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.common.param.fishball.AddFishBallGoodsParam;
import com.yeyks.common.param.fishball.UpdateFishBallGoodsParam;
import com.yeyks.commonReference.service.consumer.FishBallGoodsService;
import com.yeyks.consumer.dal.domain.FishBallGoods;
import com.yeyks.consumer.dal.domain.FishBallGoodsMessage;
import com.yeyks.consumer.service.FishBallGoodsMessageService;
import com.yeyks.web.AccountUtils;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * 鱼宝中心
 */
@Api(tags = "鱼宝商品(优惠券)接口", description = "鱼宝商品(优惠券)的相关接口[查询,添加,编辑等]")
@RestController
@RequestMapping("/ajax/fbgoods")
public class FishBallGoodsController {


    @Autowired
    private FishBallGoodsService fishBallGoodsService;

    @Autowired
    private FishBallGoodsMessageService fishBallGoodsMessageService;


    @NeedToken
    @ApiOperation("查询鱼宝商品(优惠券)信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码", name = "pageNum", paramType = "query", defaultValue = "1", dataType = "int", required = true),
            @ApiImplicitParam(value = "每页数量", name = "pageSize", paramType = "query", defaultValue = "10", dataType = "int", required = true)})
    @PostMapping("/list")
    public ResponseEntity<AjaxResponseBody> list(
                                                 @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                                 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {

        AccountInfo loginUser = AccountUtils.getLoginUser();

        return fishBallGoodsService.list(pageNum, pageSize, loginUser);
    }

    @NeedToken
    @ApiOperation("添加鱼宝商品(优惠券)信息")
    @PostMapping("/add")
    public ResponseEntity<AjaxResponseBody> add(@Valid AddFishBallGoodsParam addFishBallGoodsParam) {

        AccountInfo loginUser = AccountUtils.getLoginUser();

        return fishBallGoodsService.save(addFishBallGoodsParam, loginUser);
    }

    @NeedToken
    @ApiOperation("编辑鱼宝商品(优惠券)信息")
    @PostMapping("/update")
    public ResponseEntity<AjaxResponseBody> update(@Valid UpdateFishBallGoodsParam updateFishBallGoodsParam) {

        AccountInfo loginUser = AccountUtils.getLoginUser();
        if (null == loginUser) {
            throw new ServiceException("账号token过期，重新登录");
        }

        return fishBallGoodsService.update(updateFishBallGoodsParam, loginUser);
    }

    ///**
    //     * 删除标记：0；未删除  1：已删除
    //     */
    //    @ApiModelProperty("删除标记：0；未删除  1：已删除")
    //    private Integer deleteFlag;


    @NeedToken
    @ApiOperation(value = "启用和禁用鱼宝商品")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "商品ID", name = "id", paramType = "query", dataType = "int", required = true),
            @ApiImplicitParam(value = "是否启用 0禁用, 1启用", name = "status", paramType = "query", defaultValue = "1", dataType = "int", required = true)})
    @PostMapping("/enable")
    public ResponseEntity<AjaxResponseBody> enable(
                                                   @RequestParam(name = "id", defaultValue = "", required = true) Integer id,
                                                   @RequestParam(name = "status", defaultValue = "1", required = true) Integer status) {
        AccountInfo loginUser = AccountUtils.getLoginUser();
        return fishBallGoodsService.enable(loginUser, id, status);
    }


    @NeedToken
    @ApiOperation("删除鱼宝商品")
    @ApiImplicitParam(value = "商品ID", name = "id", paramType = "query", dataType = "int", required = true)
    @PostMapping("/delete")
    public ResponseEntity<AjaxResponseBody> delete(
                                                   @RequestParam(name = "id", defaultValue = "", required = true) Integer id) {

        AccountInfo loginUser = AccountUtils.getLoginUser();

        return fishBallGoodsService.delete(loginUser, id);
    }


    @NeedToken
    @ApiOperation(value = "v4.5.0 查询商品兑换记录", response = FishBallGoodsMessage.class)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码", name = "pageNum", paramType = "query", defaultValue = "1", dataType = "int", required = true),
            @ApiImplicitParam(value = "每页数量", name = "pageSize", paramType = "query", defaultValue = "10", dataType = "int", required = true)})
    @PostMapping("/record/list")
    public ResponseEntity<AjaxResponseBody> queryRecordList(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                                                            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize){

        AccountUtils.getLoginUser();
        PageResult<FishBallGoodsMessage> fishBallGoodsMessagePageResult = fishBallGoodsMessageService.queryPage(pageNum, pageSize);
        if(!CollectionUtils.isEmpty(fishBallGoodsMessagePageResult.getList())){
            fishBallGoodsMessagePageResult.getList().forEach(fishBallGoodsMessage -> {
                FishBallGoods fishBall = fishBallGoodsService.getById(fishBallGoodsMessage.getGoodsId());
                if(null != fishBall){
                    fishBallGoodsMessage.setTitle(fishBall.getTitle());
                    fishBallGoodsMessage.setNeedFishBallCount(fishBall.getNeedFishballCount());
                }
            });
        }
        //集合的相关整合
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("count", fishBallGoodsMessagePageResult.getTotal());
        resultMap.put("list", fishBallGoodsMessagePageResult.getList());

        return ResponseEntity.ok(AjaxResponseBody.success(resultMap));
    }

    //修改兑换商品的状态信息

    @NeedToken
    @ApiOperation(value = "v4.5.0 更新商品状态信息", response = Boolean.class)
    @PostMapping("/update/status")
    public AjaxResponseBody<Boolean> updateStatus(
            @ApiParam(name = "id", value = "主键ID")
            @RequestParam(value = "id") Integer id,
            @ApiParam(name = "receiveStatus", value = "1未发货，2发货，3完成", allowableValues = "1,2")
            @RequestParam(value = "receiveStatus") Integer receiveStatus){

        AccountUtils.getLoginUser();
        FishBallGoodsMessage goodsMessage = fishBallGoodsMessageService.getById(id);
        if(goodsMessage == null){
            throw new ServiceException("该ID的商品兑换记录不存在");
        }
        goodsMessage.setReceiveStatus(receiveStatus);
        boolean flag = fishBallGoodsMessageService.updateById(goodsMessage);
        return AjaxResponseBody.success(flag);
    }

}
